/*
 * Copyright and license 
 */
package gui;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * @author JuanK
 *
 */
public class FrameTreatmentOptions extends JFrame implements ActionListener, ChangeListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private MainFrame instance;
	private JButton buttonGo;
	private JButton buttonCancel;
	private JSlider sliderOption1;
	private JSlider sliderOption2;
	private JSlider sliderOption3;
	private JSlider sliderOption4;
	private JSlider sliderOption5;
	private JSlider sliderOption6;
	
	private static String ACTION_GO = "ACTION_GO";
	private static String ACTION_CANCEL = "ACTION_CANCEL";
	
	public FrameTreatmentOptions(MainFrame i)
	{
		instance = i;
		init();
	}
	
	private void init()
	{
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		this.setSize(300, 300);
		this.setLocation(screenSize.width/2 - 150, screenSize.height/2 - 100);
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.setResizable(false);
		this.setLayout(new GridLayout(7, 2));
		
		buttonGo = new JButton("Go");
		buttonGo.addActionListener(this);
		buttonGo.setActionCommand(ACTION_GO);
		
		buttonCancel = new JButton("Cancel");
		buttonCancel.addActionListener(this);
		buttonCancel.setActionCommand(ACTION_CANCEL);
		
		sliderOption1 = new JSlider(JSlider.HORIZONTAL, 0, 100, 50);
		sliderOption1.setMajorTickSpacing(25);
		sliderOption1.setSnapToTicks(true);
		sliderOption1.setPaintTicks(true);
		
		sliderOption2 = new JSlider(JSlider.HORIZONTAL, 0, 10, 5);
		sliderOption3 = new JSlider(JSlider.HORIZONTAL, 0, 10, 5);
		sliderOption4 = new JSlider(JSlider.HORIZONTAL, 0, 10, 5);
		sliderOption5 = new JSlider(JSlider.HORIZONTAL, 0, 10, 5);
		sliderOption6 = new JSlider(JSlider.HORIZONTAL, 0, 10, 5);
		
		this.add(new JLabel("Smooth"));
		this.add(sliderOption1);
		this.add(new JLabel("Option 2"));
		this.add(sliderOption2);
		this.add(new JLabel("Option 3"));
		this.add(sliderOption3);
		this.add(new JLabel("Option 4"));
		this.add(sliderOption4);
		this.add(new JLabel("Option 5"));
		this.add(sliderOption5);
		this.add(new JLabel("Option 6"));
		this.add(sliderOption6);
		
		this.add(buttonGo);
		this.add(buttonCancel);
		
	}
	

	public void actionPerformed(ActionEvent event) {
		String actionCommand = event.getActionCommand();
		if(actionCommand.equals(ACTION_GO))
		{
			this.setVisible(false);
			int value = sliderOption1.getValue();
			if(value == 0)
				value = 3;
			else if(value % 2 != 1)
				value--;
			
			try {
				instance.processImage(value);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//TODO: Check how to do it with buffers
//			instance.processImageWithBuffers(value);
		}
		else if(actionCommand.equals(ACTION_CANCEL))
		{
			instance.setEnabled(true);
			this.dispose();
		}
		
	}

	/* (non-Javadoc)
	 * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
	 */
	@Override
	public void stateChanged(ChangeEvent e) {
		// TODO Auto-generated method stub
		
	}

}
